package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: AddressBookController
 * @Author: YuanDing
 * @Date: 2024/2/7 11:50
 * @Description:
 */

@Api(tags = "用户地址本")
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 获取菜单列表
     * @return
     */
    @ApiOperation("获取用户全部地址")
    @GetMapping("/list")
    public Result list() {
        Result result = addressBookService.list();
        return result;
    }

    @ApiOperation("获取用户默认地址")
    @GetMapping("/default")
    public Result defaultAddress() {
        Result result = addressBookService.getDefaultAddress();
        return result;
    }

    @ApiOperation("添加地址")
    @PostMapping
    public Result addAddress(@RequestBody AddressBook addressBook) {
        addressBookService.addAddress(addressBook);
        return Result.success();
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result updateDefaultAddress(@RequestBody AddressBook addressBook) {
        addressBookService.updateDefaultAddress(addressBook.getId());
        return Result.success();
    }

    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result getAddressById(@PathVariable Long id) {
        Result result = addressBookService.getAddressById(id);
        return result;
    }

    @ApiOperation("修改地址")
    @PutMapping
    public Result updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping
    public Result delAddress(Long id) {
        addressBookService.delAddress(id);
        return Result.success();
    }
}
